<?php
class DataAreasManager{
	private $SystemAreas = array();
	private $UserAreas = array();
	function __construct(){
	}
	static function swap($bytebuffer){
		$obj = new DataAreasManager();
		$num_of_system_area = DataUtil::readInt32( $bytebuffer ) ;
		for($i = 0; $i<$num_of_system_area; $i++){
			$obj->SystemAreas[] = DataUtil::readBBin( $bytebuffer ) ;
		}
		$num_of_user_area = DataUtil::readInt32( $bytebuffer ) ;
		for($i = 0; $i<$num_of_user_area; $i++){
			$obj->UserAreas[] = DataUtil::readBBin( $bytebuffer ) ;
		}
		return $obj;
	}
	function getBinSize(){
		$size = 0;
		$size += 4 ;
		if(is_array($this->SystemAreas)){
			foreach($this->SystemAreas as $areaname){
				$size += 1  + count($areaname);
			}
		}
		$size += 4 ;
		if(is_array($this->UserAreas)){
			foreach($this->UserAreas as $areaname){
				$size += 1  + count($areaname);
			}
		}
		return $size;
	}
	function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeInt32( $buf , count($this->SystemAreas) ) ;
		if(is_array($this->SystemAreas)){
			foreach($this->SystemAreas as $areaname){
				DataUtil::writeBBin( $buf , $areaname ) ;
			}
		}
		DataUtil::writeInt32( $buf , count($this->UserAreas) ) ;
		if(is_array($this->UserAreas)){
			foreach($this->UserAreas as $areaname){
				DataUtil::writeBBin( $buf , $areaname ) ;
			}
		}
		return  $buf ->_array() ;
	}
	function addSystemArea($areaname){
		$this->SystemAreas[] = unpack('C*', $areaname ) ;
	}
	function addUserArea($areaname){
		$this->UserAreas[] = unpack('C*', $areaname ) ;
	}
	function getListSystemAreas(){
		$rs = array();
		if(is_array($this->SystemAreas)){
			foreach($this->SystemAreas as $areaname){
				$rs[] = implode(array_map("chr",  $areaname )) ;
			}
		}
		return $rs;
	}
	function getListUserAreas(){
		$rs = array();
		if(is_array($this->UserAreas)){
			foreach($this->UserAreas as $areaname){
				$rs[] = implode(array_map("chr",  $areaname )) ;
			}
		}
		return $rs;
	}
}
?>